package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;


    // 批量导入预约设置信息
    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否进行了设置
                System.out.println(orderSetting.getOrderDate() + "////////////");
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //说明已存在，变为更新
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                } else {
                    //没有设置，进行设置
                    orderSettingMapper.add(orderSetting);
                }

            }

        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        System.out.println(date + "impl111");

        String begin = date + "-1";
        String end = date + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        //调用mapper，根据日期范围查询预约设置数据
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map<String, Object> m = new HashMap<>();
                m.put("date", orderSetting.getOrderDate().getDate());//获取日期的数字
                m.put("number", orderSetting.getNumber());
                m.put("reservations", orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    //根据日期设置对应的预约设置数据
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //根据日期进行查询是否已经进行了预约设置
        long count = orderSettingMapper.findCountByOrderDate(orderDate);
        if (count > 0) {
            //当前日期已经进行预约设置，进行更新操作
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        } else {
            //没有进行预约设置，进行插入操作
            orderSettingMapper.add(orderSetting);
        }

    }


}
